package com.fc.test.api;


import com.fc.test.util.JsonUtil;
import io.swagger.annotations.*;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Configuration
@Controller
@RequestMapping("apiUploadImgController")
@Api(value="文件上传")
public class ApiUploadImgController {

    @Value("${web.upload-path:#{null}}")
    private String uploadPath;

    public String getUploadPath() {
        return uploadPath;
    }

    public void setUploadPath(String uploadPath) {
        this.uploadPath = uploadPath;
    }

    // 文件大小限制
    private String fileSizeLimit;

    @ResponseBody
    @PostMapping(value = "/uploadFile",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "上传图片", notes = "上传图片", httpMethod="POST" ,consumes="multipart/form-data")
    public String uploadFile(
            HttpServletRequest request,
            @ApiParam(value = "上传图片文件", required = true) @RequestParam("file") MultipartFile multipartFile
    ) throws IOException{
        Map<String,Object> resMap = new HashMap<>();
        if (request instanceof MultipartHttpServletRequest) {
            String originalFilename = multipartFile.getOriginalFilename();
            if(!"".equals(originalFilename)){
                HttpSession session = request.getSession();
                String uploadDir = "/userfiles" + "/";
                // 每天上传的文件根据日期存放在不同的文件夹
                String autoCreatedDateDirByParttern = "yyyy" + "/" + "MM" + "/" + "dd" + "/";
                String autoCreatedDateDir = DateFormatUtils.format(new java.util.Date(), autoCreatedDateDirByParttern);
                String ctxDir = uploadPath;
                /*if (!ctxDir.endsWith(String.valueOf("/"))) {
                   *//* ctxDir = ctxDir + File.separatorChar;*//*
                    ctxDir = ctxDir + "/";
                }*/
                String saveDirectory = ctxDir + uploadDir + autoCreatedDateDir;
                File savePath = new File(saveDirectory);
                if (!savePath.exists()) {
                    savePath.mkdirs();
                }

                String imgUrl = UUID.randomUUID().toString().replaceAll("-", "")+ originalFilename.substring(originalFilename.lastIndexOf("."));
                String newFilePath = saveDirectory + imgUrl;
                try {
                    multipartFile.transferTo(new File(newFilePath));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                resMap.put("picPath","/apiabbFiles" + uploadDir + autoCreatedDateDir+imgUrl);
                resMap.put("success",true);
                resMap.put("msg","上传图片成功！");
            }else{
                resMap.put("success",false);
                resMap.put("msg","请选择图片！");
            }

        }else{
            resMap.put("success",false);
            resMap.put("msg","参数错误！");
        }
        return JsonUtil.jsonMapStr(resMap);
    }

  /*  @RequestMapping("deleteImgById.client")
    public void deleteImgById(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> resMap = new HashMap<>();
        String id = Util.varFormat(request.getParameter("id"));
        ActivityPicture activityPicture = (ActivityPicture) dao.getObjectById(ActivityPicture.class,id);
        dao.delete(activityPicture);
        resMap.put("success",true);
        resMap.put("msg","删除成功！");
        HttpUtil.formatResponse(response,resMap);
    }*/

}
